using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 荆棘Buff - 受到伤害时反弹伤害给攻击���
/// </summary>
public class ThornsBuff : BuffBase
{
    private float m_ThornsDamagePercentage;
    private long m_ThornsDamageAmount;
    private bool m_UsePercentage;

    public ThornsBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
        if (buffData is ThornsData thornsData)
        {
            m_ThornsDamagePercentage = thornsData.ThornsDamagePercentage;
            m_ThornsDamageAmount = thornsData.ThornsDamageAmount;
            m_UsePercentage = thornsData.UsePercentage;
        }
        else
        {
            m_ThornsDamagePercentage = 0.3f; // 默认反弹30%伤害
            m_ThornsDamageAmount = 10; // 默认反弹10点伤���
            m_UsePercentage = true;
        }
    }

    protected override void OnStart()
    {
        base.OnStart();
        
        // 播放荆棘特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        // 设置动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("thorns", true);
        }
        
        Logger.Log($"{Target.name} 获得荆棘效果，反弹伤害给攻击���");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        // 荆棘期间不需要特殊处理，在受到伤害时触发
    }

    /// <summary>
    /// 处理荆棘反弹伤害（在受到伤害时调用）
    /// </summary>
    /// <param name="damageReceived">受到的伤���/param>
    /// <param name="attacker">攻击���/param>
    public void ProcessThornsDamage(long damageReceived, Entity attacker)
    {
        if (attacker == null || attacker.HealthComponent.IsDead) return;
        
        // 计算反弹伤害
        long thornsDamage;
        if (m_UsePercentage)
        {
            thornsDamage = (long)(damageReceived * m_ThornsDamagePercentage);
        }
        else
        {
            thornsDamage = m_ThornsDamageAmount;
        }
        
        // 对攻击者造成反弹伤害
        attacker.HealthComponent.TakeDamage(thornsDamage);
        
        Logger.Log($"{Target.name} 的荆棘效果对 {attacker.name} 造成 {thornsDamage} 点反弹伤���");
        
        // 触发伤害事件
        var args = ReferencePool.Acquire<EntityTakeDamageEventArgs>();
        args.Entity = attacker;
        args.Damage = thornsDamage;
        args.Attacker = Target;
        EventHelper.Fire(this, args);
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        Logger.Log($"{Target.name} 荆棘效果结束");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        
        // 恢复动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("thorns", false);
        }
        
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("Thorns"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return true; // 荆棘效果可以叠加，刷新持续时���
    }
}
